package com.dygg.bigfish.controller;

import com.alibaba.fastjson2.JSON;
import com.dygg.bigfish.dao.UserDao;
import com.dygg.bigfish.entity.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "userServlet", value = "/userServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("op");
        if ("register".equals(op)){
            register(request,response);
        }else if ("login".equals(op)){
            login(request,response);
        }else if ("logout".equals(op)){
            logout(request,response);
        }else if ("revise".equals(op)){
            revise(request,response);
        }
    }

    protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //调取userDao中的注册方法
        UserDao userDao = new UserDao();
        boolean flag = false;
        if (userDao.queryAll(username)){
            flag = userDao.addUser(username,password);
        }
        PrintWriter writer = response.getWriter();
        writer.println(flag);
        writer.flush();
        writer.close();
    }

    protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        boolean flag = true;
        HttpSession session = request.getSession();
        session.removeAttribute("user");//销毁user
        PrintWriter out = response.getWriter();
        String str = JSON.toJSONString(flag);
        out.println(str);//返回前端
        out.flush();
        out.close();
    }


    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");//获取username参数
        String password = request.getParameter("password");//获取password参数
        boolean isRemember = Boolean.parseBoolean(request.getParameter("isRemember"));

        UserDao userDao = new UserDao();
        User user = userDao.login(username, password);//调用LoginDao里的方法
        HttpSession session = request.getSession();
        session.setAttribute("user", user);//通过session储存user信息

        //加入cookie
        if (isRemember) {
            Cookie usernameCookie = new Cookie("username", username);
            usernameCookie.setMaxAge(7 * 24 * 60 * 60); // 设置cookie 最大寿命 单位 秒
            response.addCookie(usernameCookie);

            Cookie userpasswordCookie = new Cookie("password", password);
            userpasswordCookie.setMaxAge(7 * 24 * 60 * 60); // 设置cookie 最大寿命 单位 秒
            response.addCookie(userpasswordCookie);

            Cookie checkbox = new Cookie("checkbox", "1");
            checkbox.setMaxAge(7 * 24 * 60 * 60);
            response.addCookie(checkbox);
        } else {
            Cookie usernameCookie = new Cookie("username", username);
            usernameCookie.setMaxAge(0); // 设置cookie 最大寿命 单位 秒
            response.addCookie(usernameCookie);

            Cookie userpasswordCookie = new Cookie("password", password);
            userpasswordCookie.setMaxAge(0); // 设置cookie 最大寿命 单位 秒
            response.addCookie(userpasswordCookie);

            Cookie checkbox = new Cookie("checkbox", "1");
            checkbox.setMaxAge(0);
            response.addCookie(checkbox);
        }
        PrintWriter out = response.getWriter();
        String str = JSON.toJSONString(user);
        out.println(str);//返回前端
        out.flush();
        out.close();
    }
    protected void revise(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String oldPassword = request.getParameter("oldPassword");
        String newPassword = request.getParameter("newPassword");
        UserDao userDao = new UserDao();
         userDao.queryUserID(id);
        boolean flag =false;
        if (userDao.queryUserID(id).equals(oldPassword)){
                flag = userDao.modify(id,newPassword);
        }
        PrintWriter writer = response.getWriter();
/*        userDao.modify(user);
        writer.println(flag);*/
        writer.println(flag);
        writer.flush();
        writer.close();
    }
    }